home *** CD-ROM | disk | FTP | other *** search
/ Software of the Month Club 2000 October / Software of the Month - Ultimate Collection Shareware 277.iso / pc / PROGRAMS / UTILITY / WINLINUX / DATA1.CAB / usr_-_Usr_Files / BIN / XFERSTAT.{DH < prev    next >
Text File  |  1999-09-17  |  9KB  |  312 lines

  1. #! /usr/bin/perl
  2. # ---------------------------------------------------------------------------
  3. #
  4. # USAGE: xferstats <options>
  5. #
  6. # OPTIONS:
  7. #       -f <filename>   Use <filename> for the log file
  8. #       -r              include real users 
  9. #       -a              include anonymous users 
  10. #       -h        include report on hourly traffic
  11. #       -d        include report on domain traffic
  12. #       -t        report on total traffic by section
  13. #       -D <domain>     report only on traffic from <domain>
  14. #       -l <depth>      Depth of path detail for sections
  15. #       -s <section>    Section to report on, For example: -s /pub will report
  16. #                only on paths under /pub
  17. #
  18. # ---------------------------------------------------------------------------
  19.  
  20. # edit the next two lines to customize for your domain.
  21. # This will allow your domain to be seperated in the domain listing.
  22.  
  23. $mydom1 = "wustl";
  24. $mydom2 = "edu";
  25.  
  26. # edit the next line to customize for your default log file
  27. $usage_file = "/usr/adm/xferlog";
  28.  
  29. # Edit the following lines for default report settings.
  30. # Entries defined here will be over-ridden by the command line.
  31.  
  32. $opt_h = 1; 
  33. $opt_d = 0;
  34. $opt_t = 1;
  35. $opt_l = 3;
  36.  
  37. require 'getopts.pl';
  38. &Getopts('f:rahdD:l:s:');
  39.  
  40. if ($opt_r) { $real = 1;}
  41. if ($opt_a) { $anon = 1;}
  42. if ($real == 0 && $anon == 0) { $anon = 1; }
  43. if ($opt_f) {$usage_file = $opt_f;}
  44.  
  45. open (LOG,$usage_file) || die "Error opening usage log file: $usage_file\n";
  46.  
  47. if ($opt_D) {print "Transfer Totals include the '$opt_D' domain only.\n";
  48.          print "All other domains are filtered out for this report.\n\n";}
  49.  
  50. if ($opt_s) {print "Transfer Totals include the '$opt_s' section only.\n";
  51.          print "All other sections are filtered out for this report.\n\n";}
  52.  
  53. line: while (<LOG>) {
  54.  
  55.    @line = split;
  56.    next if ($#line != 16);
  57.    next if (!$anon && $line[12] eq "a");
  58.    next if (!$real && $line[12] eq "r");
  59.  
  60.    $daytime = substr($_, 0, 10) . substr($_, 19, 5);
  61.    $time = substr($_,11,2); 
  62.  
  63.    if ($line[8] eq "\.") { $line[8] = "/unreadable/filename";}
  64.    next if (substr($line[8],0,length("$opt_s")) ne "$opt_s");
  65.    $line[8] = substr($line[8],length("$opt_s"));
  66.    @path = split(/\//, $line[8]);
  67.  
  68. #
  69. # Why was the origional xferstats dropping leading 1 character path
  70. # segments???
  71. #
  72. #  while (length($path[1]) <= 1) {
  73. #     shift @path;
  74. #     next line if ($#path == -1);
  75. #  }
  76.  
  77. # Things in the top-level directory are assumed to be informational files
  78.  
  79.    if ($#path == 1)
  80.       { $pathkey = "Index/Informational Files"; }
  81.       else {
  82.     $pathkey = "";
  83.     for ($i=1; $i <= $#path-1 && $i <= $opt_l;$i++) {
  84.         $pathkey = $pathkey . "/" . $path[$i];
  85.         }
  86.     }
  87.  
  88.    $line[6] =~ tr/A-Z/a-z/;
  89.  
  90.    @address = split(/\./, $line[6]);
  91.  
  92.    $domain = $address[$#address];
  93.    if ($domain eq "$mydom2" && $address[$#address-1] eq "$mydom1")
  94.       { $domain = $mydom1 . "." . $mydom2; }
  95.    if ( int($address[0]) > 0 || $#address < 2 )
  96.       { $domain = "unresolved"; }
  97.  
  98.    $count = 1;
  99.    if ($opt_D)
  100.      {if (substr($domain,0,length("$opt_D")) eq "$opt_D" ) { $count = 1;} else
  101.      {$count = 0;}
  102.      }
  103.  
  104.  
  105.    if ($count) {
  106.  
  107.    $xferfiles++;                                # total files sent
  108.    $xfertfiles++;                               # total files sent
  109.    $xferfiles{$daytime}++;                      # files per day
  110.    $groupfiles{$pathkey}++;                     # per-group accesses
  111.    $domainfiles{$domain}++;
  112.  
  113.    $xfersecs{$daytime}    += $line[5];          # xmit seconds per day
  114.    $domainsecs{$domain}   += $line[5];        # xmit seconds for domain
  115.    $xferbytes{$daytime}   += $line[7];          # bytes per day
  116.    $domainbytes{$domain}  += $line[7];        # xmit bytes to domain
  117.    $xferbytes             += $line[7];          # total bytes sent
  118.    $groupbytes{$pathkey}  += $line[7];          # per-group bytes sent
  119.  
  120.    $xfertfiles{$time}++;                        # files per hour
  121.    $xfertsecs{$time}      += $line[5];          # xmit seconds per hour
  122.    $xfertbytes{$time}     += $line[7];          # bytes per hour
  123.    $xfertbytes            += $line[7];          # total bytes sent
  124.    }
  125. }
  126. close LOG;
  127.  
  128. @syslist = keys(systemfiles);
  129. @dates = sort datecompare keys(xferbytes);
  130.  
  131. if ($xferfiles == 0) {die "There was no data to process.\n";}
  132.  
  133.  
  134. print "TOTALS FOR SUMMARY PERIOD ", $dates[0], " TO ", $dates[$#dates], "\n\n";
  135. printf ("Files Transmitted During Summary Period  %12.0f\n", $xferfiles);
  136. printf ("Bytes Transmitted During Summary Period  %12.0f\n", $xferbytes); 
  137. printf ("Systems Using Archives                   %12.0f\n\n", $#syslist+1);
  138.  
  139. printf ("Average Files Transmitted Daily          %12.0f\n",
  140.    $xferfiles / ($#dates + 1));
  141. printf ("Average Bytes Transmitted Daily          %12.0f\n",
  142.    $xferbytes / ($#dates + 1));
  143.  
  144. format top1 =
  145.  
  146. Daily Transmission Statistics
  147.  
  148.                  Number Of    Number of    Average    Percent Of  Percent Of
  149.      Date        Files Sent  Bytes  Sent  Xmit  Rate  Files Sent  Bytes Sent
  150. ---------------  ----------  -----------  ----------  ----------  ----------
  151. .
  152.  
  153. format line1 =
  154. @<<<<<<<<<<<<<<  @>>>>>>>>>  @>>>>>>>>>>  @>>>>>>>>>  @>>>>>>>    @>>>>>>>  
  155. $date,           $nfiles,    $nbytes,     $avgrate,   $pctfiles,  $pctbytes
  156. .
  157.  
  158. $^ = top1;
  159. $~ = line1;
  160.  
  161. foreach $date ( sort datecompare keys(xferbytes) ) {
  162.  
  163.    $nfiles   = $xferfiles{$date};
  164.    $nbytes   = $xferbytes{$date};
  165.    $avgrate  = sprintf("%5.1f KB/s", $xferbytes{$date}/$xfersecs{$date}/1000);
  166.    $pctfiles = sprintf("%8.2f", 100*$xferfiles{$date} / $xferfiles);
  167.    $pctbytes = sprintf("%8.2f", 100*$xferbytes{$date} / $xferbytes);
  168.    write;
  169. }
  170.  
  171. if ($opt_t) {
  172. format top2 =
  173.  
  174. Total Transfers from each Archive Section (By bytes)
  175.  
  176.                                                  ---- Percent  Of ----
  177.      Archive Section      Files Sent Bytes Sent  Files Sent Bytes Sent
  178. ------------------------- ---------- ----------- ---------- ----------
  179. .
  180.  
  181. format line2 =
  182. @<<<<<<<<<<<<<<<<<<<<<<<< @>>>>>>>>> @>>>>>>>>>> @>>>>>>>   @>>>>>>>
  183. $section,                 $files,    $bytes,     $pctfiles, $pctbytes
  184. .
  185.  
  186. $| = 1;
  187. $- = 0;
  188. $^ = top2;
  189. $~ = line2;
  190.  
  191. foreach $section ( sort bytecompare keys(groupfiles) ) {
  192.  
  193.    $files = $groupfiles{$section};
  194.    $bytes = $groupbytes{$section};
  195.    $pctbytes = sprintf("%8.2f", 100 * $groupbytes{$section} / $xferbytes);
  196.    $pctfiles = sprintf("%8.2f", 100 * $groupfiles{$section} / $xferfiles);
  197.    write;
  198.  
  199. }
  200.  
  201. if ( $xferfiles < 1 ) { $xferfiles = 1; }
  202. if ( $xferbytes < 1 ) { $xferbytes = 1; }
  203. }
  204.  
  205. if ($opt_d) {
  206. format top3 =
  207.  
  208. Total Transfer Amount By Domain
  209.  
  210.              Number Of    Number of     Average    Percent Of  Percent Of
  211. Domain Name  Files Sent   Bytes Sent   Xmit  Rate  Files Sent  Bytes Sent
  212. -----------  ----------  ------------  ----------  ----------  ----------
  213. .
  214.  
  215. format line3 =
  216. @<<<<<<<<<<  @>>>>>>>>>  @>>>>>>>>>>>  @>>>>>>>>>  @>>>>>>>    @>>>>>>>  
  217. $domain,     $files,     $bytes,       $avgrate,   $pctfiles,  $pctbytes
  218. .
  219.  
  220. $- = 0;
  221. $^ = top3;
  222. $~ = line3;
  223.  
  224. foreach $domain ( sort domnamcompare keys(domainfiles) ) {
  225.  
  226.    if ( $domainsecs{$domain} < 1 ) { $domainsecs{$domain} = 1; }
  227.  
  228.    $files = $domainfiles{$domain};
  229.    $bytes = $domainbytes{$domain};
  230.    $avgrate  = sprintf("%5.1f KB/s",
  231.                   $domainbytes{$domain}/$domainsecs{$domain}/1000);
  232.    $pctfiles = sprintf("%8.2f", 100 * $domainfiles{$domain} / $xferfiles);
  233.    $pctbytes = sprintf("%8.2f", 100 * $domainbytes{$domain} / $xferbytes);
  234.    write;
  235.  
  236. }
  237.  
  238. print "\n";
  239. print "These figures only reflect ANONYMOUS FTP transfers.  There are many\n";
  240. print "sites which mount the archives via NFS, and those transfers are not\n";
  241. print "logged and reported by this program.\n\n";
  242.  
  243. }
  244.  
  245. if ($opt_h) {
  246.  
  247. format top8 =
  248.  
  249. Hourly Transmission Statistics
  250.  
  251.                  Number Of    Number of    Average    Percent Of  Percent Of
  252.      Time        Files Sent  Bytes  Sent  Xmit  Rate  Files Sent  Bytes Sent
  253. ---------------  ----------  -----------  ----------  ----------  ----------
  254. .
  255.  
  256. format line8 =
  257. @<<<<<<<<<<<<<<  @>>>>>>>>>  @>>>>>>>>>>  @>>>>>>>>>  @>>>>>>>    @>>>>>>>  
  258. $time,           $nfiles,    $nbytes,     $avgrate,   $pctfiles,  $pctbytes
  259. .
  260.  
  261.  
  262. $| = 1;
  263. $- = 0;
  264. $^ = top8;
  265. $~ = line8;
  266.  
  267. foreach $time ( sort keys(xfertbytes) ) {
  268.  
  269.    $nfiles   = $xfertfiles{$time};
  270.    $nbytes   = $xfertbytes{$time};
  271.    $avgrate  = sprintf("%5.1f KB/s", $xfertbytes{$time}/$xfertsecs{$time}/1000);
  272.    $pctfiles = sprintf("%8.2f", 100*$xfertfiles{$time} / $xferfiles);
  273.    $pctbytes = sprintf("%8.2f", 100*$xfertbytes{$time} / $xferbytes);
  274.    write;
  275. }
  276. }
  277. exit(0);
  278.  
  279. sub datecompare {
  280.  
  281.    $date1  = substr($a, 11, 4) * 4800;
  282.    $date2  = substr($b, 11, 4) * 4800;
  283.    $date1 += index("JanFebMarAprMayJunJulAugSepOctNovDec",substr($a, 4, 3))*400;
  284.    $date2 += index("JanFebMarAprMayJunJulAugSepOctNovDec",substr($b, 4, 3))*400;
  285.    $date1 += substr($a, 8, 2);
  286.    $date2 += substr($b, 8, 2);
  287.    $date1 - $date2;
  288.  
  289. }
  290.  
  291. sub domnamcompare {
  292.  
  293.    $sdiff = length($a) - length($b);
  294.    ($sdiff < 0) ? -1 : ($sdiff > 0) ? 1 : ($a lt $b) ? -1 : ($a gt $b) ? 1 : 0;
  295.  
  296. }
  297.  
  298. sub bytecompare {
  299.  
  300.    $bdiff = $groupbytes{$b} - $groupbytes{$a};
  301.    ($bdiff < 0) ? -1 : ($bdiff > 0) ? 1 : ($a lt $b) ? -1 : ($a gt $b) ? 1 : 0;
  302.  
  303. }
  304.  
  305. sub faccompare {
  306.  
  307.    $fdiff = $fac{$b} - $fac{$a};
  308.    ($fdiff < 0) ? -1 : ($fdiff > 0) ? 1 : ($a lt $b) ? -1 : ($a gt $b) ? 1 : 0;
  309.  
  310. }
  311.  
  312.